package com.hn658.base.message.eventbus.handler.groupchat;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hn658.base.message.common.dto.NotificationDTO;
import com.hn658.base.message.common.enums.EventType;
import com.hn658.base.message.common.enums.NotifySenderType;
import com.hn658.base.message.common.enums.NotifyType;
import com.hn658.base.message.eventbus.event.groupchat.GroupMsgEvent;
import com.hn658.base.message.eventbus.exception.EventbusException;
import com.hn658.base.message.eventbus.handler.AbstractEventHandler;
import com.hn658.base.message.eventbus.util.NotifiConvert;
import com.hn658.base.message.group.business.IGroupManager;
import com.hn658.base.message.group.entity.MemberEO;
import com.hn658.base.message.notification.msg.entity.MessageEO;
import com.hn658.base.message.notification.notify.business.INotificationManager;
import com.hn658.base.message.notification.notify.entity.NotificationEO;
@Component
public class GroupMessageHandler extends AbstractEventHandler<GroupMsgEvent>{

	@Autowired
    private INotificationManager notificationManager;
	
	@Autowired
	private IGroupManager groupManager;
	
	@Override
	public void handle(GroupMsgEvent event) {
		MessageEO msg = event.getMessage();
        NotificationEO notificationEO = NotifiConvert.convert(msg,event.getEventId());
        NotificationEO notifyDb = notificationManager.find(msg.getNotifyId());
        if(notifyDb == null){
            notificationManager.save(notificationEO);
        }else{
            notificationManager.update(notificationEO);
        }
        List<MemberEO> memberList = null;
        if(msg.getToGroupId()!=null&&msg.getAppId()!=null){
        	//查询群成员列表
        	memberList = groupManager.queryGroupMemberList(msg.getAppId(), msg.getToGroupId());
        }
        if(memberList!=null){
        	for(MemberEO member:memberList){
        		NotificationDTO notificationDTO = NotifiConvert.convert(msg,notificationEO);
        		//不会给发送用户推送消息
        		//if(notificationDTO.getFromUid().longValue()!=member.getUid().longValue()){
    			//判断是否正在聊天
    			boolean isTalking = notificationManager.currentTalk(msg.getAppId(), member.getUid(), member.getGroupId()+"", NotifyType.GroupChat);
    			notificationDTO.setSenderType(NotifySenderType.Comet.getValue());
        		notificationDTO.setToUid(member.getUid());
        		int unReadCount = notificationManager.getUnReadCountByNotiId(msg.getAppId(), member.getUid(), msg.getNotifyId());
    	        if(isTalking){
    	        	//是当前聊天，未读数为0,
    	        	notificationDTO.setUnReadCount(0);
    	        }else{
    	        	//不是当前聊天，更新设置未读数
    	        	unReadCount++;
    	        	notificationDTO.setUnReadCount(unReadCount);
    	        	notificationManager.saveUnReadCountByNotiId(msg.getAppId(), member.getUid(), msg.getNotifyId(), unReadCount);
    	        }
        		notificationManager.send(notificationDTO, event.getIsOfflineSend());
        	}
        }else{
        	throw new EventbusException(EventbusException.EmptyToUserError);
        }
	}

	@Override
	public EventType getTarget() {
		return EventType.NormalGroupChat;
	}
	
	
}
